using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Weathertastic
{
    class ParseWeather
    {
        private WeatherInfo _weatherInfo;

        public ParseWeather(string sXmlFile) 
        {
            _weatherInfo = new WeatherInfo();
            try
            {
                XmlDocument XmlDoc = new XmlDocument();
                XmlDoc.Load(sXmlFile);
                XmlNode NodeLocation = XmlDoc.SelectSingleNode("//dnam");
                XmlNode NodeCondition = XmlDoc.SelectSingleNode("//cc/t");
                XmlNode NodeTemperature = XmlDoc.SelectSingleNode("//cc/tmp");
                XmlNode NodeIcon = XmlDoc.SelectSingleNode("//cc/icon");

                XmlNode NodeTomorrowHigh = XmlDoc.SelectSingleNode("/weather/dayf/day[2]/hi");
                XmlNode NodeTomorrowLow = XmlDoc.SelectSingleNode("/weather/dayf/day[2]/low");

                XmlNode NodeTomorrowDayIcon = XmlDoc.SelectSingleNode("/weather/dayf/day[2]/part[1]/icon");
                XmlNode NodeTomorrowNightIcon = XmlDoc.SelectSingleNode("/weather/dayf/day[2]/part[2]/icon");

                _weatherInfo.LocationName = NodeLocation.InnerText;
                _weatherInfo.Condition = NodeCondition.InnerText;
                _weatherInfo.Temperature = NodeTemperature.InnerText;
                // TODO: NodeIcon.InnerText can be "-" - handle here
                _weatherInfo.Icon = Convert.ToInt16(NodeIcon.InnerText);

                _weatherInfo.TomorrowHigh = NodeTomorrowHigh.InnerText;
                _weatherInfo.TomorrowLow = NodeTomorrowLow.InnerText;
                _weatherInfo.TomorrowDayIcon = Convert.ToInt16(NodeTomorrowDayIcon.InnerText);
                _weatherInfo.TomorrowNightIcon = Convert.ToInt16(NodeTomorrowNightIcon.InnerText);

                LogConditionIcon.Log(_weatherInfo.Condition, _weatherInfo.Icon);

            }
            catch (FileNotFoundException exc)
            {
                //throw;
            }

        }

        public WeatherInfo WeatherInfo
        {
            get { return this._weatherInfo; }
        }

    }
}
